<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>browser</title>
</head>

<body>
    <div id="root"></div>
    <ul>
        <li><a onclick="pushA()">/a</a></li>
        <li><a onclick="pushB()">/b</a></li>
        <li><a onclick="pushC()">/c</a></li>
        <li><a onclick="forward()">forward</a></li>
        <li><a onclick="back()">back</a></li>
        <li><a onclick="go(-1)">go-1=back</a></li>
        <li><a onclick="go(1)">go+1=forward</a></li>
    </ul>
    <script>
        let history = window.history;
        (function (history) {
            let oldPushState = history.pushState;
            history.pushState = function (state, title, pathname) {
                let result = oldPushState.apply(history, arguments);
                if (typeof window.onpushstate === 'function') {
                    window.onpushstate(new CustomEvent('pushstate', { detail: { pathname, state } }));
                }
            }
            let oldReplaceState = history.replaceState;
            history.pushState = function (state, title, pathname) {
                let result = oldReplaceState.apply(history, arguments);
                if (typeof window.onpushstate === 'function') {
                    window.onpushstate(new CustomEvent('pushstate', { detail: { pathname, state } }));
                }
            }
        })(history);
        window.onpushstate = (event) => {
            console.log(event);
            root.innerHTML = window.location.pathname;
        }
        window.onpopstate = (event) => {
            console.log(event);
            root.innerHTML = window.location.pathname;
        }
        function pushA() {
            history.pushState({ name: 'a' }, null, '/a');
        }
        function pushB() {
            history.pushState({ name: 'b' }, null, '/b');
        }
        function pushC() {
            history.pushState({ name: 'c' }, null, '/c');
        }
        function forward() {
            history.forward();
        }
        function back() {
            history.back();
        }
        function go(step) {
            history.go(step);
        }
    </script>
</body>

</html>